<?php
/**
 * 材料模型
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\SupplierM;
use Illuminate\Database\Eloquent\Model;

class ProductMaterial extends Model{

    protected $table = 'product_material';

    protected $guarded = [];

    //报价单位
    public static $_quote_currency = [
        1  => '码',
        2  => '米',
        3  => '个',
        4  => '件',
        5  => '公斤',
        6  => '条',
    ];
    //后整理
    public static $_test_after = [
        1  => '涂层',
        2  => '贴合',
        3  => '泼水',
        4  => '压光',
        5  => '压花',
        6  => '印花',
        7  => '普通整理',
        8  => '功能整理',
        9  => '无',
    ];
    //功能性
    public static $_function = [
        1  => '抗紫外线50+',
        2  => '抗菌功能',
        3  => '凉感功能',
        4  => '吸湿速干',
        5  => '环保再生',
        6  => '液氨整理',
        7  => '消臭功能',
        8  => '智能温控',
        9  => 'SORONA',
        10  => 'COUDURA',
    ];
    //成分
    public static $_composition = [
        1  => '聚酯纤维',
        2  => '锦纶',
        3  => '氨纶',
        4  => '棉',
        5  => '亚麻',
        6  => '桑蚕丝',
        7  => '绵羊毛',
        8  => '莫代尔',
        9  => '莱赛尔',
        10  => '天丝',
    ];
    //门幅单位
    public static $_door_unit = [
        1  => 'INCH',
        2  => 'CM',
    ];

    const AUDIT_NO = 1;      //未审核
    const AUDIT_SUCCESS = 2; //审核通过
    const AUDIT_REJECT = 3;  //审核拒绝
    public static $_auditStatus = [
        self::AUDIT_NO      => '待审核',
        self::AUDIT_SUCCESS => '通过',
        self::AUDIT_REJECT  => '拒绝',
    ];

    const ORIGIN_IN = 1;      //待寻源
    const ORIGIN_SUCCESS = 2; //寻源成功
    const ORIGIN_ERROR = 3;  //寻源失败
    public static $_originStatus = [
        self::ORIGIN_IN      => '待寻源',
        self::ORIGIN_SUCCESS => '寻源成功',
        self::ORIGIN_ERROR   => '寻源失败'
    ];

    protected $casts = [
        //'function' => 'array',//功能性
        //'composition' => 'json',//成分

        'container_no' => 'array', //货柜号
        'material_application_classify_id' => 'array', //应用品类
    ];

    const STATUS_YES = 1; //启用
    const STATUS_NO  = 2; //停用
    public static $_status = [
        self::STATUS_YES => '已使用',
        self::STATUS_NO  => '未使用',
    ];

    //是否入库
    const IN_YES = 1; //已入库
    const IN_NO  = 2; //未入库
    public static $flag_status = [
        self::IN_YES => '是',
        self::IN_NO  => '否',
    ];
    //是否评审
    const REVIEW_YES = 1; //是
    const REVIEW_NO  = 2; //否
    public static $review_status = [
        self::IN_YES => '是',
        self::IN_NO  => '否',
    ];

    public function material_classify_max(){
        return $this->belongsTo(ProductMaterialClassifyMax::class, 'material_classify_max_id');
    }

    public function material_classify_min(){
        return $this->belongsTo(ProductMaterialClassifyMin::class, 'material_classify_min_id');
    }

    //public function material_application_classify(){
    //    return $this->belongsTo(ProductMaterialApplicationClassify::class, 'material_application_classify_id');
    //}

    public function supplier(){
        return $this->belongsTo(SupplierM::class,'supplier_id');
    }

    ////第三个参数是定义此关联的模型(ProductMaterial)在连接表(product_supplier_m)里的外键名，第四个参数是另一个模型(ProductSupplier)在连接表里的外键名
    //public function supplier_m(){
    //    return $this->belongsToMany(SupplierM::class,'product_supplier_m','material_id','supplier_id')
    //        ->using(ProductSupplierM::class)
    //        ->withPivot('supplier_material_no', 'title', 'quote_price', 'quote_currency', 'audit_status', 'audit_time');
    //}

    public static function options($where = []){
        $query = $where ? self::query()->where(formatCondition($where)) : self::query();
        $res = $query->get()->map(function ($model){
            return collect(['material' => $model->material_code.'-'.$model->title,
                'id' => $model->id]);
        });
        return $res->pluck('material','id');
    }

    public static function getModelById($id){
        return self::query()->where('id', $id)->first();
    }

    public static function getModelByCode($code){
        return self::query()->where('material_code',$code)->first();
    }

    public static function getValueByCode($code,$field){
        return self::query()->where('material_code',$code)->value($field);
    }

    public static function getValueById($id,$field){
        return self::query()->where('id',$id)->value($field);
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            if($model->status == 1){
                throw new \Exception('材料已启用请勿删除');
            }
            $count = ProductSupplierM::query()->where('material_id',$model->id)->count();
            if($count > 0){
                throw new \Exception('请先删除所有关联的材料说明书');
            }
        });
    }
}
